import Vue from 'vue'
import Router from 'vue-router'
import store from '@/service/store'
import * as types from '@/service/types'

import Index from '@/components/Index'
import Home from '@/components/home/Home'
import Main from '@/components/home/Main'
import Message from '@/components/home/Message'
import AccountHome from '@/components/account/AccountHome'
import Login from '@/components/account/Login'
import Register from '@/components/account/Register'
import PostHome from '@/components/post/PostHome'
import PostList from '@/components/post/PostList'
import PostView from '@/components/post/PostView'
import UserHome from '@/components/user/UserHome'
import UserCenter from '@/components/user/UserCenter'
import UserSetting from '@/components/user/UserSetting'
import EditPost from '@/components/user/EditPost'
import ListPost from '@/components/user/PostList'
import ListMsg from '@/components/user/MsgList'
import ListUser from '@/components/user/UserList'
import ListNotice from '@/components/user/NoticeList'

Vue.use(Router)

const router = new Router({
  routes: [
    {
      path: '/',
      name: 'Index',
      component: Index,
      children: [{
        path: '',
        redirect: '/home'
      }, {
        path: '/post',
        name: 'PostHome',
        component: PostHome,
        children: [{
          path: 'list/:module',
          name: 'PostList',
          component: PostList
        }, {
          path: 'view/:id',
          name: 'PostView',
          component: PostView
        }]
      }, {
        path: '/home',
        name: 'Home',
        component: Home,
        children: [{
          path: '',
          redirect: 'main'
        }, {
          path: 'main',
          name: 'Main',
          component: Main
        }, {
          path: 'message',
          name: 'Message',
          component: Message
        }]
      }]
    }, {
      path: '/account',
      name: 'AccountHome',
      component: AccountHome,
      children: [{
        path: '',
        redirect: 'login'
      }, {
        path: 'login',
        name: 'Login',
        component: Login
      }, {
        path: 'register',
        name: 'Register',
        component: Register
      }]
    }, {
      path: '/user',
      name: 'UserHome',
      component: UserHome,
      meta: {
        requireAuth: true
      },
      children: [{
        path: '',
        redirect: 'center'
      }, {
        path: 'center',
        name: 'UserCenter',
        component: UserCenter
      }, {
        path: 'setting',
        name: 'UserSetting',
        component: UserSetting
      }, {
        path: 'edit-post',
        name: 'EditPost',
        component: EditPost
      }, {
        path: 'post/:who',
        name: 'ListPost',
        component: ListPost
      }, {
        path: 'msg/:who',
        name: 'ListMsg',
        component: ListMsg
      }, {
        path: 'list',
        name: 'ListUser',
        component: ListUser
      }, {
        path: 'notice-list',
        name: 'ListNotice',
        component: ListNotice
      }, {
        path: 'setting',
        name: 'UserSetting',
        component: UserSetting
      }]
    }]
})

// 页面刷新时，重新赋值token
let jwt = window.localStorage.getItem('jwt')
if (jwt) {
  let exp = window.localStorage.getItem('exp')
  if (new Date().getTime() + 5000 < exp) {
    store.commit(types.LOGIN, jwt)
    store.commit(types.EXP, exp)
  } else {
    store.commit(types.LOGOUT)
  }
}

let user = window.localStorage.getItem('user')
if (user) {
  store.commit(types.USER, JSON.parse(user))
}

router.beforeEach((to, from, next) => {
  if (store.state.jwt) {
    if (to.name === 'Login') {
      next('/user')
    } else {
      next()
    }
  } else if (to.matched.some(r => r.meta.requireAuth)) {
    next({
      path: '/account',
      query: {redirect: to.fullPath}
    })
  } else {
    next()
  }
})

export default router
